home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 051-075 / 069 / monproc / getprocs.c next >
C/C++ Source or Header  |  1995-03-13  |  4KB  |  150 lines

  1. /* getprocs.c - Grab all available processes and return them to you in a
  2.  *         simple exec list. The list is made up of  nodes--the "name"
  3.  *         fields pointing to the process structure. 
  4.  *
  5.  *      Phillip Lindsay (c) 1987 Commodore-Amiga Inc. 
  6.  * You may use this source as long as the copyright notice is left intact.
  7.  */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/nodes.h>
  11. #include <exec/lists.h>
  12. #include <exec/memory.h>
  13. #include <exec/tasks.h>
  14. #include <exec/execbase.h>
  15. #include <libraries/dos.h>
  16. #include <libraries/dosextens.h>
  17. #include <libraries/filehandler.h>
  18.  
  19. #ifdef MANX
  20. #include <functions.h>
  21. #include <stdio.h>
  22. #else
  23. #include <lattice/stdio.h>
  24. #endif
  25.  
  26. /* getnode() will allocate a node structure for you. And initialize the node
  27.  * structure with the given values.
  28.  */       
  29. struct Node *getnode(name,type,pri)
  30. char *name;
  31. UBYTE type,pri;
  32. {
  33.  register struct Node *mynode;
  34.  
  35.  mynode = (struct Node *) 
  36.   AllocMem((ULONG)sizeof(*mynode),MEMF_PUBLIC | MEMF_CLEAR); 
  37.  
  38.  if(!mynode) return((struct Node *)NULL);
  39.  
  40.  mynode->ln_Name = name;
  41.  mynode->ln_Type = type;
  42.  mynode->ln_Pri  = pri;
  43.  
  44.  return(mynode);
  45. }
  46.  
  47. /* freenode() frees a given Exec node. 
  48.  * Make sure you remove node from any list. 
  49.  */
  50. void freenode(mynode)
  51. struct Node *mynode;
  52. {
  53.  FreeMem(mynode,(ULONG)sizeof(*mynode));
  54. }
  55.  
  56.  
  57. /* getprocs() will grab all processes in the system task's list and
  58.  *  append an exec node to a given list. The node name field filled
  59.  *  with a pointer to the process strcuture. 
  60.  * WARNING: This isn't a casual subroutine. The returned list is valid as
  61.  * long as a process is not ended or added after this subroutine is
  62.  * called. To be real safe you should probably check to see if the
  63.  * process is still around before trying to look at the data structure.  
  64.  */
  65. void getprocs(plist)
  66. struct List *plist;    /* passed a pointer to a initialized exec list */ 
  67. {
  68.  extern   struct ExecBase       *SysBase;
  69.  register struct ExecBase      *execbase=SysBase;
  70.  register struct List          *proclist;
  71.  register struct Node           *aproc,*bproc;
  72.  register USHORT          count=0;
  73.  
  74. /* I haven't clocked this code, but you shouldn't as a rule stay disabled for
  75.  * no more than 250ms. I'm have no doubt I'm illegal, but you can't be
  76.  * too safe when dealing with something that changes with a blink of a
  77.  * interrupt.
  78.  */
  79.  
  80.  Disable();
  81.  
  82. /* first we deal with ourselves... */
  83.  if(execbase->ThisTask->tc_Node.ln_Type == NT_PROCESS)
  84.   {
  85.    if((bproc=getnode(execbase->ThisTask,0,0)))
  86.     {
  87.      AddTail(plist,bproc);
  88.     }
  89.   }
  90.  
  91. /* hopefully the added braces make the flow clear */
  92.  for(;count <= 1;count++)
  93.   {
  94.    if(!count) 
  95.     proclist = &execbase->TaskReady;
  96.    else
  97.     proclist = &execbase->TaskWait;
  98.  
  99.    if(proclist->lh_TailPred != proclist)
  100.     {
  101.      for(aproc = proclist->lh_Head;aproc->ln_Succ;aproc=aproc->ln_Succ)
  102.       { 
  103.        if(aproc->ln_Type == NT_PROCESS)
  104.         { 
  105.          if((bproc=getnode(aproc,0,0)))
  106.           {
  107.            AddTail(plist,bproc);
  108.           }
  109.         }
  110.       }
  111.     }    
  112.   }
  113.  
  114.  Enable();
  115.  
  116. } /* end of getprocs() */
  117.  
  118.  
  119. /* freeprocs() will free all nodes in a given list. Function assumes nodes where
  120.  *    initialized with getnode().
  121.  */
  122. void freeprocs(plist)
  123. struct List *plist;
  124. {
  125.  register struct Node *proc;
  126.  
  127.  while((proc=RemTail(plist)))
  128.   freenode(proc);
  129. }
  130.  
  131. /*
  132. main()
  133.  {
  134.   struct List    procs;
  135.   struct Node    *proc;
  136.  
  137.   NewList(&procs);    ** Initialize list header **
  138.   getprocs(&procs);    ** Fill list **
  139.  
  140.  ** print any processess in list.... if any **
  141.    if(procs.lh_TailPred != &procs)
  142.     for(proc = procs.lh_Head;proc->ln_Succ;proc=proc->ln_Succ)
  143.      puts( ((struct Process *)proc->ln_Name)->pr_Task.tc_Node.ln_Name );
  144.      
  145.    freeprocs(&procs);
  146.  }
  147.  
  148. */
  149.  
  150.